package com.wym.charging.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wym.charging.common.Result;
import com.wym.charging.dto.ChargeDto;
import com.wym.charging.dto.GetChargingRecordDto;
import com.wym.charging.entity.ChargingRecord;
import com.wym.charging.service.ChargingRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 充电记录(ChargingRecord)表控制层
 *
 * @author WangYimin
 * @since 2024-12-08 22:40:50
 */
@RestController
@RequestMapping("charging-record")
@Tag(name = "充电记录")
public class ChargingRecordController {
    /**
     * 服务对象
     */
    @Autowired
    private ChargingRecordService chargingRecordService;

    @PostMapping("/charge")
    @Operation(summary = "充电")
    public Result<String> charge(@RequestBody ChargeDto chargeDto) throws Exception {
        if (chargeDto.getUserId() == null || chargeDto.getUserId() <= 0) {
            throw new Exception("用户错误");
        }
        if (chargeDto.getPowerStripNumber() == null || chargeDto.getPowerStripNumber() <= 0) {
            throw new Exception("未选排插");
        }
        if (chargeDto.getChargingOptionsId() == null || chargeDto.getChargingOptionsId() <= 0) {
            throw new Exception("未选充电选项");
        }
        chargingRecordService.charge(chargeDto);
        return Result.success("充电成功");
    }

    @Operation(summary = "获取充电记录")
    @PostMapping("/get-charging-record")
    public Result<Page<ChargingRecord>> getChargingRecord(@RequestBody GetChargingRecordDto getChargingRecordDto) {

        Page<ChargingRecord> powerStripPage = new Page<>(getChargingRecordDto.getCurrentPage() <= 0 ? 1 : getChargingRecordDto.getCurrentPage(), getChargingRecordDto.getPageSize() <= 0 ? 10 : getChargingRecordDto.getPageSize());
        LambdaQueryWrapper<ChargingRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (getChargingRecordDto.getUserId() != null) {
            queryWrapper.eq(ChargingRecord::getUserId, getChargingRecordDto.getUserId());
        }
        Page<ChargingRecord> page = chargingRecordService.page(powerStripPage, queryWrapper);
        return Result.success(page);
    }
}

